//
//  RunLoop.h
//  WiFiMACSim
//
//  Created by Christopher Kemsley on 3/31/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#ifndef WiFiMACSim_RunLoop_h
#define WiFiMACSim_RunLoop_h

#include <map>
#include <set>
#include "Types.hpp"

SimNamespaceBegin







class	RunLoop ;
class	RunLoopSource ;





class	RunLoop
{
public:
	
	typedef enum {
		kModeDefault	=	2 ,
		
		kModeTime		=	0 ,	// ONLY for the system. NO TOUCHING!!
		kModeSystem		=	1 ,	// Runs before 'default'
		kModeCleanup	=	3	// Runs after 'default'
	} Mode ;
	
	
	static RunLoop&	currentRunLoop () ;
	
	
	/** Adding/Removing Sources **/
	
	void	addSource		( RunLoopSource * source , Mode mode = kModeDefault ) ;
	void	removeSource	( RunLoopSource * source , Mode mode = kModeDefault ) ;
	
	
	/** Running the loop **/
	
	void	runAllModesRepeatedly ( UInteger count ) ;
	void	runAllModes () ;
	void	runMode ( Mode mode ) ;
	
	
	
	
private:
	typedef std::set<RunLoopSource*>	Sources ;
	typedef std::map<Mode,Sources>		SourceMap ;
	
	SourceMap	mSources ;
	
	RunLoop () ;
	~RunLoop () ;
} ;






class	RunLoopSource
{
public:
	virtual void	runFromRunLoop ( RunLoop & sender , RunLoop::Mode mode ) = 0 ;
} ;





SimNamespaceEnd
#endif
